from django.db import models
from django.contrib.auth.models import Group, Permission
from django.db.models.signals import post_syncdb


def init_permissions(sender, **kwargs):
	try:
		admins = Group.objects.get(name='Admins')
		pass
	except:
		admins = Group(name='Admins')
		admins.save()
		admins.permissions.add(
			Permission.objects.get(codename='create_society'),
			Permission.objects.get(codename='edit_society'),
			Permission.objects.get(codename='delete_society'),
			Permission.objects.get(codename='create_member'),
			Permission.objects.get(codename='remove_member'),
			Permission.objects.get(codename='promote_to_admin'),
			Permission.objects.get(codename='promote_to_manager'),
			Permission.objects.get(codename='demote_to_manager'),
			Permission.objects.get(codename='demote_to_user'),
			Permission.objects.get(codename='add_event'),
			Permission.objects.get(codename='change_event'),
			Permission.objects.get(codename='delete_event'),
			Permission.objects.get(codename='create_post'),
			Permission.objects.get(codename='edit_post'),
			Permission.objects.get(codename='delete_post')
		)
		admins.save()
	
	try:
		managers = Group.objects.get(name='Managers')
		pass
	except:
		managers = Group(name='Managers')
		managers.save()
		managers.permissions.add(
			Permission.objects.get(codename='create_society'),
			Permission.objects.get(codename='edit_society'),
			Permission.objects.get(codename='create_member'),
			Permission.objects.get(codename='add_event'),
			Permission.objects.get(codename='change_event'),
			Permission.objects.get(codename='delete_event'),
			Permission.objects.get(codename='create_post'),
			Permission.objects.get(codename='edit_post'),
			Permission.objects.get(codename='delete_post')
		)
		managers.save()
	
	try:
		users = Group.objects.get(name='Users')
		pass
	except:
		users = Group(name='Users')
		users.save()
		users.permissions.add(
			Permission.objects.get(codename='create_society'),
			Permission.objects.get(codename='create_member'),
		)
		users.save()


post_syncdb.connect(init_permissions)
